/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.woodws.fresh.im.service.impl

import com.woodws.fresh.common.core.domain.R
import com.woodws.fresh.common.core.extra.copyProperties
import com.woodws.fresh.common.data.beetl.service.impl.BaseServiceImpl
import com.woodws.fresh.im.dao.ImMessageDao
import com.woodws.fresh.im.api.dto.ImMessageDto
import com.woodws.fresh.im.entity.ImMessage
import com.woodws.fresh.im.api.service.ImMessageService
import org.apache.dubbo.config.annotation.DubboService
import org.springframework.stereotype.Service

/**
 * 消息表业务实现
 * @author maoxiaodong
*/
@DubboService
@Service
class ImMessageServiceImpl: ImMessageService, BaseServiceImpl<ImMessageDto, ImMessage, ImMessageDao>() {
    override fun findByFromIdGroupByToId(fromId: Long, num: Long): R<List<ImMessageDto>> {
        return R.success(dao.findByFromIdGroupByToId(fromId, num).map { it.copyProperties(ImMessageDto::class) })
    }

    /**
     * 查询聊天信息
     * @param fromId 我的ID
     * @param toId 好友ID
     */
    override fun findByFromIdAndToId(fromId: Long, toId: Long): R<List<ImMessageDto>>{
        return R.success(dao.findByFromIdAndToId(fromId, toId).map{entityToDto(it)})
    }

    /**
     * 未读的信息
     * @param 接收人ID
     */
    override fun findUnreadByToId(toId: Long): R<List<ImMessageDto>>{
        return R.success(dao.findUnreadByToId(toId).map{entityToDto(it)})
    }

    /**
     * 根据id更新状态
     * @param id 主键
     * @param status 状态
     */
    override fun updateStatusById(id: Long, status: String): R<Int>{
        return R.success(dao.updateStatusById(id, status))
    }
}